/*
 * @Descripttion:
 * @version:
 * @Author: lwj
 * @Date: 2022-05-26 13:54:03
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2022-12-09 11:22:53
 */

import { reqParkList, reqOrgList, reqSpaceManager } from "@/api/api";
import { jsonp } from 'vue-jsonp' //使用jsonp

export default {
    data() {
        return {
            parkList: [],
            projectList: [], //单位数据
            buildings: [],
            loadingReset: false,
            loadingSearch1: false,
            sysInstanceIndex: 1,
            sysInstanceTotalPages: 10,
            flag: true,
            sysInstanceIndex2: 1,
            sysInstanceTotalPages2: 10,
            sysInstanceIndex3: 1,
            sysInstanceTotalPages3: 10,
            loading: false,
            loading2: false,
            loading3: false,

            getAddressflag: false, //地图选点
            orgName: '',
            buildingsName: '',
            floorName: '',
            selectedValue: "",
            buildingsName2: "",
            floorName2: "",
        };
    },
    mounted() {
        // this.SpaceManager3();
        this.addListenAddress();
    },
    methods: {
        // 清空
        handleClear() {
            this.selectedValue = '';
            this.orgName = '';
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.projectList = []
            this.OrgList()
        },
        // 远程搜索
        async remoteMethod(query) {
            this.selectedValue = query //保留输入的值
            this.orgName = query; //保留输入的值
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.projectList = []
            this.OrgList()
        },
        // 单位操作
        async loadMore() {
            if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10)) return
            this.sysInstanceIndex++;
            this.loading = true;
            // 获取列表数据的方法
            await this.OrgList()
            this.loading = false;
        },
        // 获取单位列表
        async OrgList() {
            var res = await reqOrgList({
                userId: this.user.userId,
                roleName: this.user.roleName,
                tenantId: this.user.tenantId,
                orgName: this.orgName
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.projectList = res.data.rows
        },
        // 下拉请求列表
        enterpriseChange(val) {
            if (val && this.flag) {
                this.OrgList();
                this.flag = false; //只执行一次
            }
        },
        // 根据单位查询建筑物
        projectchange(e) {
            this.searchForm.orgId = e;
            // 清空建筑物
            this.searchForm.parentId = "";
            this.handleClear2();
            // 清空楼层
            this.searchForm.spaceId = '';
            this.handleClear3();
            // if (e != '') {
            //     this.SpaceManager3();
            // }
            this.searchClick1();
        },
        // 单位操作结束

        // 清空
        handleClear2() {
            this.buildingsName = '';
            this.buildingsName2 = '';
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []
            if (this.searchForm.orgId != '') {
                this.SpaceManager3()
            }
        },
        // 远程搜索
        async remoteMethod2(query) {
            this.buildingsName = query;
            this.buildingsName2 = query;
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []
            this.SpaceManager3()
        },
        // 建筑物操作
        async loadMore2() {
            if (this.sysInstanceIndex2 >= Math.ceil(this.sysInstanceTotalPages2 / 10)) return
            this.sysInstanceIndex2++;
            this.loading2 = true;
            // 获取列表数据的方法
            await this.SpaceManager3()
            this.loading2 = false;
        },
        // 建筑物列表
        async SpaceManager3() {
            let res = await reqSpaceManager(this.sysInstanceIndex2, 10, {
                userId: this.user.userId,
                roleName: this.user.roleName,
                metaCode: 1,
                orgId: this.searchForm.orgId,
                isStatistics: 0,
                name: this.buildingsName2
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages2 = res.data.total
            this.buildings = [...this.buildings, ...res.data.rows]
        },
        // 根据建筑物查询楼层
        projectchange2(e) {
            this.searchForm.parentId = e
            this.searchForm.spaceId = ''
            this.handleClear3()
                // if (e != '') {
                //     this.SpaceManager();
                // }
            this.searchClick1();
        },
        // 建筑物操作结束

        // 清空
        handleClear3() {
            this.floorName = '';
            this.floorName2 = '';
            this.sysInstanceIndex3 = 1;
            this.sysInstanceTotalPages3 = 10;
            this.floor = []
            if (this.searchForm.parentId != '') {
                this.SpaceManager()
            }
        },
        // 远程搜索
        async remoteMethod3(query) {
            this.floorName = query;
            this.floorName2 = query;
            this.sysInstanceIndex3 = 1;
            this.sysInstanceTotalPages3 = 10;
            this.floor = []
            this.SpaceManager()
        },
        projectchange3(e) {
            this.searchForm.spaceId = e;
            this.searchClick1();
        },
        // 楼层操作
        async loadMore3() {
            if (this.sysInstanceIndex3 >= Math.ceil(this.sysInstanceTotalPages3 / 10)) return
            this.sysInstanceIndex3++;
            this.loading3 = true;
            // 获取列表数据的方法
            await this.SpaceManager()
            this.loading3 = false;
        },
        // 楼层列表
        async SpaceManager() {
            let res = await reqSpaceManager(this.sysInstanceIndex3, 10, {
                userId: this.user.userId,
                roleName: this.user.roleName,
                metaCode: 2,
                orgId: this.searchForm.orgId,
                parentId: this.searchForm.parentId,
                isStatistics: 0,
                name: this.floorName2
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages3 = res.data.total
            this.floor = [...this.floor, ...res.data.rows]
        },


        // 获取单位id
        // changeOrgId(id) {
        //     console.log(id);
        // },
        // 获取园区列表
        // async getreqParkList2() {
        //     let res = await reqParkList(1, 999, {
        //         userId: this.user.userId,
        //         roleName: this.user.roleName,
        //     });
        //     if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
        //     this.parkList = res.data.rows;
        // },
        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            this.loadingSearch1 = true;
            // 如果只选了建筑物就是建筑物的id,选了楼层就是传楼层的id
            if (this.searchForm.spaceId != null && this.searchForm.spaceId != '') {
                this.parentId = this.searchForm.spaceId;
            } else {
                this.parentId = this.searchForm.parentId;
            }
            await this.getreqParkList();
            this.loadingSearch1 = false;
            this.clearSelections(); //清空复选框
        },
        // 高级搜索重置
        async resetClick() {
            this.searchForm = {
                orgId: "",
                parentId: "",
                floorId: "",
            };
            this.search_val = "";
            this.page = 1;
            this.limit = 10;
            this.loadingReset = true;
            await this.getreqParkList();
            this.loadingReset = false;
        },
        // 导入
        importClick() {
            this.$refs.refImportDevice.getProductList();
            this.$refs.refImportDevice.getManufacturerList();
            this.$refs.refImportDevice.RegionList();
            this.$refs.refImportDevice.closeAddDialog2(); // 防止弹出验证
            this.$refs.refImportDevice.dialogVisible = true;
        },
        // 监听获取经纬度
        addListenAddress() {
            window.addEventListener(
                "message",
                (event) => {
                    // console.log(event);
                    // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
                    let loc = event.data;
                    // console.log(loc);
                    // console.log(loc.module);
                    if (loc && loc.module == "locationPicker") {
                        //防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
                        // console.log(loc, "loc");
                        this.getAddress(loc.latlng.lat, loc.latlng.lng, loc.poiname);
                        // this.isShowMap = false
                    }
                },
                false
            );
        },
        // 获取地址信息
        getAddress(lat, lng, poiname) {
            jsonp(
                    `https://apis.map.qq.com/ws/geocoder/v1/?location=${lat},${lng}&key=44FBZ-OVKKP-T23D5-VGHUE-XDXC3-BDF53`, {
                        output: "jsonp",
                    }
                )
                .then((res) => {
                    // console.log(res);
                    this.getAddressflag = !this.getAddressflag;
                    let { province, city, district } = res.result.ad_info;
                    let { title } = res.result.address_reference.town;
                    // console.log(res.result, "res.result.ad_info");
                    this.$set(this.getLocationInfo, "getLat", lat);
                    this.$set(this.getLocationInfo, "getLng", lng);
                    this.$set(this.getLocationInfo, "province", province);
                    this.$set(this.getLocationInfo, "city", city);
                    this.$set(this.getLocationInfo, "district", district);
                    this.$set(this.getLocationInfo, "street", title);
                    // 详细位置
                    this.$set(
                        this.getLocationInfo,
                        "getAdd",
                        this.stripscript(poiname).length > 20 ?
                        this.stripscript(poiname).substr(0, 20) :
                        this.stripscript(poiname)
                    );
                })
                .catch((err) => {
                    // console.log(err, "err");
                });
        },
        // 过滤特殊字符
        stripscript(s) {
            let pattern = new RegExp(
                "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）&mdash;—|{}【】‘；：”“'。，、？]"
            );
            let rs = "";
            for (let i = 0; i < s.length; i++) {
                rs = rs + s.substr(i, 1).replace(pattern, "");
            }
            return rs;
        },
    },
};